/**
 * 
 */
package com.lazyshopkeeper.front.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lazyshopkeeper.front.apiService.IItemService;
import com.lazyshopkeeper.front.common.Constant;
import com.lazyshopkeeper.front.common.DianneituiguangUtil;
import com.lazyshopkeeper.front.service.IInnerStoreService;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;

/**
 * @author xuyuqi
 *
 */
@Controller
@RequestMapping("innerStore")
public class InnerStoreController {

	private static Logger logger = LoggerFactory.getLogger(InnerStoreController.class);
	
	
	@Autowired
	private IItemService itemService;
	
	@Autowired
	private IInnerStoreService iInnerStoreService;
	
	@RequestMapping("")
	public String page(Model model, HttpSession session){
		logger.debug("进入店内推广页面");
		
		String sessionKey = (String)session.getAttribute(Constant.SESSION_KEY);
		
		model.addAttribute("gaga", "haha");
		return "innerStore";
	}
	
	
	@RequestMapping("/getinnserStroeBody")
	public 
	@ResponseBody String getInnerStroeBody(String styleType,String  tuiguangName,
			String showType,String sortType,int cosCount,int rowCount,Model model, HttpSession session) throws ApiException{
		logger.debug("进入店内推广自动植入代码");
		
		String str="";
		
		String sessionKey = (String)session.getAttribute(Constant.SESSION_KEY);
		List<Item> litems=itemService.getItemsforOnsale(sortType,sessionKey);
		
		litems=iInnerStoreService.getDesc(styleType, tuiguangName, showType, sortType, cosCount, rowCount, litems, sessionKey);

		for(int i=0;i<litems.size();i++){
			
			Item item=itemService.updateItemByDesc(litems.get(i), sessionKey);
			double radio =i/litems.size();
		
			session.setAttribute("innerStroeProgress",(int)Math.round(radio*100));
			if(item==null)
					str+=litems.get(i).getTitle()+"</br>";
		}
		return str;
	}
	
	
	@RequestMapping("/getProgress")
	public 
	@ResponseBody int getInnerStroeProgress(Model model, HttpSession session) throws ApiException{
		logger.debug("进入获得进度");
		if(session.getAttribute("innerStroeProgress")==null)
			return 0;
		else
			return Integer.parseInt(session.getAttribute("innerStroeProgress")+"");
	}
	
}
